package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.beans.EvaluationBean;
import com.tongyi.lizhishop.cache.RxCacheManager;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.common.RecycleViewActivity;
import com.tongyi.lizhishop.constants.Constants;
import com.tongyi.lizhishop.managers.APIServiceManager;
import com.tongyi.lizhishop.views.StarView;
import com.tongyi.lizhishop.views.StarViewSmall;
import com.zchu.rxcache.RxCache;
import com.zchu.rxcache.data.CacheResult;
import com.zchu.rxcache.stategy.CacheStrategy;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class EvaluationActivity extends RecycleViewActivity implements MultiItemTypeAdapter.OnItemClickListener {


    private MultiItemTypeAdapter adapter;
    private List itemDatas = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBarView(titlebar, "我收到的评价");
        initView();

    }


    @Override
    public void loadData() {
        RxCache rxCache = RxCacheManager.getRxCache();
        APIServiceManager.getInstance().create(MessageApi.class)
                .showEvaluate(SPUtils.getInstance().getString("userid"), Constants.perPageSize, currentPageIndex)
                .compose(rxCache.<EvaluationBean>transformObservable("evaluation", EvaluationBean.class, CacheStrategy.firstRemote()))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<CacheResult<EvaluationBean>>() {
                    @Override
                    public void onSuccess(CacheResult<EvaluationBean> data) {
                        EvaluationBean value = data.getData();
                        if (data == null) {
                            onFailure(null);
                            return;
                        }
                        if (data.getData().getArray().size() == 0) {
                            multipleStatusView.showEmpty();
                        }

                        if (currentPageIndex == 0) {
                            itemDatas.clear();
                            itemDatas.add(value.getAvgnum());
                        }
                        calculatePageIndexAndControlRefreshLayout(value.getArray());
                        itemDatas.addAll(value.getArray());
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        showError("请检查网络");
                    }
                });
    }

    private void initView() {
        adapter = new MultiItemTypeAdapter(this, itemDatas);
        adapter.addItemViewDelegate(new ItemViewDelegate() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.activity_evaluation_recycleview_item;
            }

            @Override
            public boolean isForViewType(Object item, int position) {
                return position == 0;
            }

            @Override
            public void convert(ViewHolder holder, Object o, int position) {
                float starNum = (float) o;

                StarView ratingBar = (StarView) holder.getView(R.id.ratingBar);
                ratingBar.setStarLight((int) (starNum + 0.5));
            }
        });
        adapter.addItemViewDelegate(new ItemViewDelegate() {
            @Override
            public int getItemViewLayoutId() {
//                return R.layout.activity_mywallet_recommand_item;
                return R.layout.fragment_evaluation_recycleview_item;
            }

            @Override
            public boolean isForViewType(Object item, int position) {
                return position != 0;
            }

            @Override
            public void convert(ViewHolder holder, Object o, int position) {
                EvaluationBean.ArrayBean arrayBean = (EvaluationBean.ArrayBean) o;
                ImageView userHeader = (ImageView) holder.getView(R.id.shadowImageHeader);
//                holder.setText(R.id.userName,arrayBean.get)
                StarViewSmall ratingBar = (StarViewSmall) holder.getView(R.id.ratingBar);
                ratingBar.setStarLight((int) (Integer.parseInt(arrayBean.getEva_num()) + 0.5));
                Glide.with(EvaluationActivity.this).load(APIServiceManager.server_address + arrayBean.getImage()).into(userHeader);
                holder.setText(R.id.centerTv, arrayBean.getEva_content());
            }
        });
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        refreshLayout.autoRefresh();
    }


    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

  /*  XLHRatingBar ratingBar = (XLHRatingBar) findViewById(R.id.ratingBar);
    ratingBar.setCountNum(5);
    ratingBar.setCountSelected(1);
    ratingBar.setOnRatingChangeListener(new XLHRatingBar.OnRatingChangeListener() {
        @Override
        public void onChange(int countSelected) {
            tvResult.setText(countSelected + "");
        }
    });*/
}
